<?php

include_once "Model.php";
//include_once "Track.php";
class Artist_ extends Model {

    public function Artist_($attr=null){ $this->table_name = ARTIST_TABLE; parent::__construct($attr); }
    
    /*
     * N.B.
     * @return: SQL query result
     */
    public function getProductionList(){
      //$tracks = new Track_(array(artist_id=>$this->attributes['artist_id']));
      //$track_list = $tracks->get(null,'GROUP BY release_id,title ORDER BY ');
      $q = "SELECT t.title,t.time_added,t.id,t.played_times,r.id AS release_id,
            r.title AS release_title,u2f.user_id AS fav_user
            
            FROM ".TRACK_TABLE." t
            LEFT JOIN ".RELEASE_TABLE." r ON (t.release_id = r.id)
            LEFT JOIN ".USER2FAV_TABLE." u2f ON ( u2f.track_id = t.id )
            WHERE t.artist_id = {$this->attributes['id']}
            
            ORDER BY r.title ASC,t.played_times DESC,t.time_added DESC";
      
      return $this->SQL->query($q);
      
    }
    
    public function getReleases(){
      $releases = array();
      $q = "SELECT COUNT(DISTINCT t.id) AS tracks_no, SUM(t.played_times) AS total_played, r.id,
            r.title, COUNT(DISTINCT u.user_id) AS favs_no
            
            FROM ".TRACK_TABLE." t
            LEFT JOIN ".RELEASE_TABLE." r ON (t.release_id = r.id)
            LEFT JOIN ".USER2FAV_TABLE." u ON (t.id = u.track_id)
                
            WHERE t.artist_id = {$this->attributes['id']}
            GROUP BY t.release_id
            ORDER BY r.title ASC,t.played_times DESC,t.time_added DESC";
      
      $this->SQL->query($q);
      
      while($release=$this->SQL->fetchObject()){
        array_push($releases,$release);
      }
      
      return $releases;
      
    }
    
    public function getDetails(){
       $q = "SELECT a. * ,SUM(t.played_times) AS total_played, COUNT(DISTINCT r.id) AS release_no,
            COUNT(DISTINCT t.id) AS tracks_no, COUNT(DISTINCT u.user_id) AS favs_no
            FROM ".ARTIST_TABLE." a
            LEFT JOIN ".TRACK_TABLE." t ON (t.artist_id=a.id)
            LEFT JOIN ".USER2FAV_TABLE." u ON (t.id = u.track_id)
            LEFT JOIN ".RELEASE_TABLE." r ON (t.release_id = r.id )
            WHERE a.id = t.artist_id AND a.id = {$this->attributes['id']}
            GROUP BY a.id";
      
      return $this->SQL->query($q)->fetchObject();
      
    }
}


?>
